package top.huntwolf.admin.uploadImgTest;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.Base64;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.junit.jupiter.api.Test;
import top.huntwolf.admin.utils.UUIDUtil;
import java.io.*;

//对七牛云图片上传方式的测试
public class test {
    // 设置需要操作的账号的AK和SK
    private static final String ACCESS_KEY = "yy_eapYydAYS62nrP74CwrP-ymIThQ7qX2qnOYAE";
    private static final String SECRET_KEY = "kFD9PmdS8LOSlWgxu4zNfdeNBdcxbVhbn_bbsXBM";
    // 要上传的空间
    private static final String bucketname = "yiqima";
    // 密钥
    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    //默认不指定key的情况下，以文件内容的hash值作为文件名
    private static final String key = null;
    //新建空间时，七牛云分配出的域名
    private static final String DOMAIN = "http://r7wr2teig.hn-bkt.clouddn.com/";
    public static String getUpToken() {
        return auth.uploadToken(bucketname, key, 3600, new StringMap().put("insertOnly", 1));
    }

    @Test
    void uploadImgCommon(){
        File file = new File("C:\\Users\\蔡晓娜\\Pictures\\1.png");
        InputStream stream = null;
        try {
            stream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        // 创建上传对象
        UploadManager uploadManager = new UploadManager(cfg);
        String upToken = auth.uploadToken(bucketname);;
        try {
            Response response = uploadManager.put(stream, key, upToken,null,null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            System.out.println(DOMAIN + putRet.key);

        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }

    @Test
    void uploadImgBase64() {
        try {
            File file =  new File("C:\\Users\\蔡晓娜\\Pictures\\1.png");
            InputStream stream = null;
            try {
                stream = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            long length = file.length();
            byte[] bytes = new byte[(int)length];
            stream.read(bytes);
            String Key = UUIDUtil.getUUID();
            try {
                String file64 = Base64.encodeToString(bytes, 0);
                Integer len = bytes.length;

                //华南空间使用 upload-z2.qiniu.com
                String url = "http://upload-z2.qiniu.com/putb64/" + len + "/key/" + UrlSafeBase64.encodeToString(Key);

                RequestBody rb = RequestBody.create(null, file64);
                Request request = new Request.Builder()
                        .url(url)
                        .addHeader("Content-Type", "application/octet-stream")
                        .addHeader("Authorization", "UpToken " + getUpToken())
                        .post(rb).build();
                OkHttpClient client = new OkHttpClient();

                okhttp3.Response response = client.newCall(request).execute();
                //用此地址可在网页中访问到上传的图片
                System.out.println(DOMAIN + Key);
                stream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
